﻿#include "DemoItemManager.h"
#include "GraphicsDef.h"
#include "BaseGraphicsItem.h"
#include "StraightLineGraphicsItem.h"
#include "ParallelLineGraphicsItem.h"
#include "RectGraphicsItem.h"
#include "EllipseGraphicsItem.h"
#include "InCircleGraphicsItem.h"
#include "CircleGraphicsItem.h"
#include "ThreePointCircleGraphicsItem.h"
#include "AngleGraphicsItem.h"
#include "PointGraphicsItem.h"
#include "TextGraphicsItem.h"
#include <QGraphicsView>
#include <QInputDialog>

namespace OfUI {

	bool sortItemByDescZValue(BaseGraphicsItem* p1, BaseGraphicsItem* p2)
	{
		return p1->zValue() > p2->zValue();
	}

	DemoItemManager::DemoItemManager(QGraphicsScene* pScene, QObject *parent)
		: BaseItemManager(pScene, parent)
	{
	}

	DemoItemManager::~DemoItemManager()
	{
	}

	void DemoItemManager::removeSelect()
	{
		if (m_pSelectItem)
			removeItem(m_pSelectItem);
	}

	BaseGraphicsItem* DemoItemManager::select(const QPointF& point)
	{
		if (ItemType::NONE_TYPE != m_nAddType)
			return nullptr;

		BaseGraphicsItem* pFirstPriorityItem = nullptr;
		BaseGraphicsItem* pSecondPriorityItem = nullptr;
		// 按Z Value降序排列
		QList<BaseGraphicsItem*> tempItemList = m_itemList;
		qSort(tempItemList.begin(), tempItemList.end(), sortItemByDescZValue);

		for (auto iter = tempItemList.begin(); iter != tempItemList.end(); ++iter)
		{
			BaseGraphicsItem* pItem = *iter;
			BaseGraphicsItem::MouseDragType eType = pItem->select(point);

			if (BaseGraphicsItem::DRAG_EDIT == eType)
			{
				pFirstPriorityItem = pItem;
				break;
			}
			else if (BaseGraphicsItem::DRAG_MOVE == eType && !pSecondPriorityItem)
			{
				pSecondPriorityItem = pItem;
			}
		}
		return pFirstPriorityItem ? pFirstPriorityItem : pSecondPriorityItem;
	}

	BaseGraphicsItem* DemoItemManager::createItem()
	{
		BaseGraphicsItem* pItem = nullptr;

		if (m_nAddType == ItemType::STRAIGHT_LINE)
		{
			pItem = new StraightLineGraphicsItem;
		}
		else if (m_nAddType == ItemType::PARALLEL_LINE)
		{
			pItem = new ParallelLineGraphicsItem;
		}
		else if (m_nAddType == ItemType::RECT)
		{
			pItem = new RectGraphicsItem;
		}
		else if (m_nAddType == ItemType::ELLIPSE)
		{
			pItem = new EllipseGraphicsItem;
		}
		else if (m_nAddType == ItemType::INSCRIBED_CIRCLE)
		{
			pItem = new InCircleGraphicsItem;
		}
		else if (m_nAddType == ItemType::TWO_POINT_CIRCLE)
		{
			pItem = new CircleGraphicsItem;
		}
		else if (m_nAddType == ItemType::THREE_POINT_CIRCLE)
		{
			pItem = new ThreePointCircleGraphicsItem;
		}
		else if (m_nAddType == ItemType::ANGLE)
		{
			pItem = new AngleGraphicsItem;
		}
		else if (m_nAddType == ItemType::POINT)
		{
			pItem = new PointGraphicsItem;
		}
		else if (m_nAddType == ItemType::TEXT)
		{
			bool bOk;
			QString text = QInputDialog::getText(m_pScene->views().at(0), "输入文本", "", QLineEdit::Normal, "", &bOk);
			if (bOk && !text.isEmpty())
			{
				TextGraphicsItem* pTextItem = new TextGraphicsItem;
				pTextItem->setText(text);
				connect(pTextItem, &TextGraphicsItem::signalEditText, this, &DemoItemManager::slotItemEditText);
				pItem = pTextItem;
			}
		}

		if (pItem)
		{
			m_itemList.append(pItem);
			m_pScene->addItem(pItem);
			// 连接信号槽
			connect(pItem, &BaseGraphicsItem::signalAddFinished, this, &DemoItemManager::slotItemAddFinished);
			connect(pItem, &BaseGraphicsItem::signalDestroyed, this, &DemoItemManager::slotItemDestoryed);
			connect(pItem, &BaseGraphicsItem::signalStateChanged, this, &DemoItemManager::slotItemStateChanged);
			connect(pItem, &BaseGraphicsItem::signalDataChanged, this, &DemoItemManager::slotItemDataChanged);
		}

		return pItem;
	}

	void DemoItemManager::slotItemAddFinished()
	{
		BaseGraphicsItem* pItem = dynamic_cast<BaseGraphicsItem*>(sender());
		if (!pItem->isVaild())
		{
			removeItem(pItem);
		}
	}

	void DemoItemManager::slotItemDestoryed()
	{
	}

	void DemoItemManager::slotItemStateChanged(int state)
	{

	}

	void DemoItemManager::slotItemDataChanged()
	{

	}

	void DemoItemManager::slotItemEditText()
	{
		TextGraphicsItem* pTextItem = dynamic_cast<TextGraphicsItem*>(sender());

		bool bOk;
		QString text = QInputDialog::getText(m_pScene->views().at(0), "输入文本", "", QLineEdit::Normal, pTextItem->getText(), &bOk);

		if (bOk)
		{
			if (text.isEmpty())
			{
				removeItem(pTextItem);
			}
			else
			{
				pTextItem->setText(text);
			}
		}
	}

}

